package com.jia.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.dto.admin.CoordinateDto;
import com.jia.dto.admin.CouponsDto;
import com.jia.entity.Coupons;
import com.jia.entity.CouponsType;
import com.jia.service.CouoponsUserService;
import com.jia.service.CouponsService;
import com.jia.service.CouponsTypeService;
import com.jia.vo.Result;
import com.jia.vo.ResultCodeEnum;
import com.jia.vo.admin.CoordinateVo;
import com.jia.vo.admin.CouponsVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api")
@Schema(description = "优惠券管理")
public class CouponsController {

    @Autowired
    private CouponsService couponsService;

    @Autowired
    private CouponsTypeService couponsTypeService;

    @Autowired
    private CouoponsUserService couoponsUserService;



    /**
     * 列表
     * @param page
     * @param limit
     * @param couponsDto
     * @return
     */
    @GetMapping("/coupons/{page}/{limit}")
    @Operation(summary = "分页查询优惠券")
    public Result<Page<CouponsVo>> coordinateList(
            @PathVariable("page") int page, @PathVariable("limit") int limit, CouponsDto couponsDto) {
        Map<String,Object> map=new HashMap<>();
        map.put("coupons",this.couponsService.COUPONS_VO_PAGE(page,limit,couponsDto));
        map.put("couponsUser",this.couponsService.COUPONSUser_VO_PAGE(page,limit,couponsDto));
        return Result.build(map, ResultCodeEnum.SUCCESS);
    }

    /**
     * 类型
     * @return
     */

    @GetMapping("/types")
    @Operation(summary = "查询优惠券类型")
    public Result<CouponsType> coordinateList() {
        return Result.build(this.couponsTypeService.list(), ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改状态
     * @param id
     * @return
     */
    @GetMapping("/updateCouponsState/{id}")
    @Operation(summary = "修改优惠券状态")
    public Result updateCouponsState(@PathVariable("id") int id) {
        couponsService.updateState(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    /**
     * 删除卡券
     * @param id
     * @return
     */
    @GetMapping("/deleteCoupons/{id}")
    @Operation(summary = "删除卡券")
    public Result deleteBattery(@PathVariable("id") int id) {
        couponsService.removeById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }




    /**
     * 删除卡券
     * @param id
     * @return
     */
    @GetMapping("/deleteCouponsUser/{id}")
    @Operation(summary = "删除卡券记录")
    public Result deleteCouponsUser(@PathVariable("id") int id) {
        this.couoponsUserService.removeById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/addCouponsTypes")
    @Operation(summary = "添加优惠券类型")
    public Result addCouponsTypes(@RequestBody CouponsType couponsType) {
        couponsType.setCreatetime(new Date());
        couponsType.setState(0);
        couponsTypeService.save(couponsType);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @PostMapping("/getaddCoupons")
    @Operation(summary = "添加优惠券")
    public Result getaddCoupons(@RequestBody Coupons coupons) {
        coupons.setState(0);
        couponsService.save(coupons);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


}
